﻿namespace UnityEditor
{
    using System;
    using System.Collections.Generic;
    using UnityEditorInternal;
    using UnityEngine;

    internal class ObjectBrowserTreeViewDataSource : DefaultTreeViewDataSource
    {
        private static string kProjectBrowserString = "ProjectBrowser";

        public ObjectBrowserTreeViewDataSource(TreeView treeView) : base(treeView)
        {
            base.m_ShowRootNode = false;
            base.m_RootIsCollapsable = false;
            SavedSearchFilters.AddChangeListener(new System.Action(this.ReloadData));
        }

        public override bool CanBeMultiSelected(TreeView.Node node)
        {
            return (ObjectBrowser.GetItemType(node.instanceID) != ObjectBrowser.ItemType.SavedFilter);
        }

        public override bool CanBeParent(TreeView.Node node)
        {
            return (!(node is SearchFilterTreeNode) || SavedSearchFilters.AllowsHierarchy());
        }

        public override void FetchData()
        {
            base.m_RootNode = new TreeView.Node(0x7fffffff, 0, null, "Invisible Root Node");
            this.SetExpanded(base.m_RootNode, true);
            List<TreeView.Node> list = new List<TreeView.Node>();
            int assetsFolderInstanceID = GetAssetsFolderInstanceID();
            int depth = 0;
            string displayName = "Assets";
            TreeView.Node parent = new TreeView.Node(assetsFolderInstanceID, depth, base.m_RootNode, displayName);
            this.ReadAssetDatabase(parent, depth + 1);
            TreeView.Node item = SavedSearchFilters.ConvertToTreeView();
            item.parent = base.m_RootNode;
            list.Add(item);
            list.Add(parent);
            base.m_RootNode.children = list.ToArray();
            foreach (TreeView.Node node3 in base.m_RootNode.children)
            {
                bool @bool = EditorPrefs.GetBool(kProjectBrowserString + node3.displayName, true);
                this.SetExpanded(node3, @bool);
            }
            base.m_NeedRefreshVisibleFolders = true;
        }

        public static int GetAssetsFolderInstanceID()
        {
            string path = "Assets";
            return AssetDatabase.GetInstanceIDFromGUID(AssetDatabase.AssetPathToGUID(path));
        }

        public override bool IsExpandable(TreeView.Node node)
        {
            return ((node.children.Length > 0) && ((node != base.m_RootNode) || base.m_RootIsCollapsable));
        }

        public override bool IsRenamingNodeAllowed(TreeView.Node node)
        {
            if (this.IsVisibleRootNode(node))
            {
                return false;
            }
            return base.IsRenamingNodeAllowed(node);
        }

        public bool IsVisibleRootNode(TreeView.Node node)
        {
            return ((node.parent != null) && (node.parent.parent == null));
        }

        private void ReadAssetDatabase(TreeView.Node parent, int baseDepth)
        {
            IHierarchyProperty property = new HierarchyProperty(HierarchyType.Assets);
            property.Reset();
            Texture2D textured = EditorGUIUtility.FindTexture(EditorResourcesUtility.folderIconName);
            Texture2D textured2 = EditorGUIUtility.FindTexture(EditorResourcesUtility.emptyFolderIconName);
            List<TreeView.Node> visibleNodes = new List<TreeView.Node>();
            while (property.Next(null))
            {
                if (property.isFolder)
                {
                    TreeView.Node item = new TreeView.Node(property.instanceID, baseDepth + property.depth, null, property.name) {
                        icon = !property.hasChildren ? textured2 : textured
                    };
                    visibleNodes.Add(item);
                }
            }
            HierarchyPropertyTreeViewDataSource.SetChildParentReferences(visibleNodes, parent);
        }

        public override bool SetExpandedByInstanceID(int instanceID, bool expand)
        {
            if (!base.SetExpandedByInstanceID(instanceID, expand))
            {
                return false;
            }
            InternalEditorUtility.expandedProjectWindowItems = base.expandedInstanceIDs.ToArray();
            foreach (TreeView.Node node in base.m_RootNode.children)
            {
                if (node.instanceID == instanceID)
                {
                    EditorPrefs.SetBool(kProjectBrowserString + node.displayName, expand);
                }
            }
            return true;
        }
    }
}

